﻿// Copyright 2009 Mike Geise
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
//     http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

namespace Falcon.Core.Controllers
{
	using System.Web.Mvc;
	using Falcon.Core.Domain;
	using Falcon.Core.Domain.Services;
	using Falcon.Core.Domain.Values;
	using Falcon.Core.Extensions;
	using Falcon.Core.Validation;

	[Authorize]
	public class SystemSettingController : DefaultController
	{
		public SystemSettingController(ISettingService settingService)
		{
			this.SettingService = settingService;
		}

		public ISettingService SettingService
		{
			get;
			private set;
		}

		[AcceptVerbs(HttpVerbs.Get)]
		public ActionResult Index(int? page)
		{
			var settings = SettingService.GetPaged(new SettingSpecification
			{
				Page = page,
				Limit = Settings.PageLimit
			});

			var setting = settings.FirstOrDefault();

			return setting.CanView ? View(Views.Index, Masters.System, settings) : NotAuthorized();
		}

		[AcceptVerbs(HttpVerbs.Get)]
		public ActionResult Create()
		{
			var setting = SettingService.Create();

			return setting.CanCreate ? View(Views.Create, Masters.System, new SettingCreate()) : NotAuthorized();
		}

		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult Create(SettingCreate value)
		{
			try
			{
				var setting = SettingService.Create();

				if (!setting.CanCreate)
				{
					return NotAuthorized();
				}

				this.SettingService.Insert(setting, value);
				this.SettingService.Rebuild();

				return Message(Routes.SystemSettingUpdate, new { id = setting.Id }, Resources.Messages.SettingCreated.Fmt(setting.Title));
			}
			catch (ValidationException exception)
			{
				ModelState.CopyValidationExceptions(exception);
			}

			return View(Views.Create, Masters.System, value);
		}

		[AcceptVerbs(HttpVerbs.Get)]
		public ActionResult Update(int id)
		{
			var setting = SettingService.GetById(id);

			if (setting == null)
			{
				return Error(Resources.Messages.SettingNotFound);
			}

			return setting.CanUpdate ? View(Views.Update, Masters.System, new SettingUpdate(setting)) : NotAuthorized();
		}

		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult Update(SettingUpdate value)
		{
			try
			{
				var setting = SettingService.GetById(value.Id);

				if (setting == null)
				{
					return Error(Resources.Messages.SettingNotFound);
				}

				if (!setting.CanUpdate)
				{
					return NotAuthorized();
				}

				this.SettingService.Update(setting, value);
				this.SettingService.Rebuild();

				return Message(Routes.SystemSettingUpdate, new { id = setting.Id }, Resources.Messages.SettingUpdated.Fmt(setting.Title));
			}
			catch (ValidationException exception)
			{
				ModelState.CopyValidationExceptions(exception);
			}

			return View(Views.Update, Masters.System, value);
		}

		[AcceptVerbs(HttpVerbs.Get)]
		public ActionResult Delete(int id)
		{
			var setting = this.SettingService.GetById(id);

			if (setting == null)
			{
				return base.Error(Resources.Messages.SettingNotFound);
			}

			return setting.CanDelete ? base.View(Views.Delete, Masters.System, new SettingDelete(setting)) : NotAuthorized();
		}

		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult Delete(SettingDelete value)
		{
			try
			{
				var setting = this.SettingService.GetById(value.Id);

				if (setting == null)
				{
					return base.Error(Resources.Messages.SettingNotFound);
				}

				if (!setting.CanDelete)
				{
					return NotAuthorized();
				}

				this.SettingService.Delete(setting);

				return base.RedirectToRoute(Routes.SystemSettingIndex);
			}
			catch (ValidationException exception)
			{
				base.ModelState.CopyValidationExceptions(exception);
			}

			return base.View(Views.Delete, Masters.System, value);
		}
	}
}